<p:config xmlns:p="http://www.orbeon.com/oxf/pipeline"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xhtml="http://www.w3.org/1999/xhtml"
    xmlns:oxf="http://www.orbeon.com/oxf/processors" xmlns:xdb="http://orbeon.org/oxf/xml/xmldb"
    xmlns:xu="http://www.xmldb.org/xupdate" xmlns:xforms="http://www.w3.org/2002/xforms">

	<p:param type="input" name="instance"/>
	<p:param type="output" name="data"/>

	<!--read global parameters-->
	<p:processor name="oxf:identity">
		<p:input name="data" href="/config/config.xml"/>
		<p:output name="data" id="global_pref"/>
	</p:processor>

	<!--load translation-->
	<p:processor name="oxf:pipeline">
		<p:input name="data" href="#global_pref#xpointer(/globalpref/lang)"/>
		<p:input name="config" href="/apps/engine/utils/XMLtranslation.xpl"/>
		<p:output name="data" id="translation"/>
	</p:processor>

	<p:processor name="oxf:request">
		<p:input name="config">
			<config >
				<include>/request/parameters</include>
			</config>
		</p:input>
		<p:output name="data" id="request"/>
	</p:processor>

	<p:processor name="oxf:xslt">
		<p:input name="data" href="#request#xpointer(/request/parameters)"/>
		<p:output name="data" id="success"/>
		<p:input name="config">
			<xsl:stylesheet version="2.0">
				<xsl:template match="/">
					<message>
						<xsl:if test="parameters/parameter[./name = 'success']/value = 1">The user has been successfuly created.</xsl:if>
					</message>
				</xsl:template>
			</xsl:stylesheet>
		</p:input>
	</p:processor>

	<p:choose href="#instance">
		<p:when test="not(exists(/null))">
			<p:processor name="oxf:xslt">
				<p:input name="data" href="#instance"/>
				<p:output name="data" id="query"/>
				<p:input name="config">
					<xdb:query xsl:version="2.0" collection="/db/portal">
						<xsl:text>xquery version "1.0";</xsl:text>
						let $user := /users/user[@id='<xsl:value-of select="/user/username"/>'] return
						<session>
						{if ($user/password/text() = string(util:md5(string('<xsl:value-of select="/user/password"/>')))) then <authenticated>true</authenticated>
							else <authenticated>false</authenticated>}
							<username>
								<xsl:value-of select="/user/username"/>
							</username>
							<level>{$user/level/text()}</level>
							<lang>{$user/lang/text()}</lang>
							<groups current="gr_0">
								<group id="gr_0">
									<name>Personal</name>
								</group>
								{for $gr in /groups/group[members/member/@id = '<xsl:value-of select="/user/username"/>'] return 
									element group{
										attribute id{$gr/@id},
										attribute level{$gr/members/member[@id='<xsl:value-of select="/user/username"/>']/@level},
										/groups/group[@id=string($gr/@id)]/name,
										/groups/group[@id=string($gr/@id)]/interface
									}
								}
							</groups>
						</session>
					</xdb:query>
				</p:input>
			</p:processor>

			<p:processor name="oxf:xmldb-query">
				<p:input name="datasource" href="/config/datasource.xml"/>
				<p:input name="query" href="#query"/>
				<p:output name="data" id="session"/>
			</p:processor>

			<p:choose href="#session">
				<p:when test="/session/authenticated = 'true'">
					<p:processor name="oxf:scope-serializer">
						<p:input name="config">
							<config>
								<key>session</key>
								<scope>session</scope>
								<session-scope>application</session-scope>
							</config>
						</p:input>
						<p:input name="data" href="#session"/>
					</p:processor>
					<p:processor name="oxf:identity">
						<p:input name="data">
							<error/>                   
						</p:input>
						<p:output name="data" id="error"/>
					</p:processor>
					<p:processor name="oxf:redirect">
						<p:input name="data">
							<redirect-url>
								<path-info>/portal/main/</path-info>
							</redirect-url>
						</p:input>
					</p:processor>
				</p:when>
				<p:otherwise>
					<p:processor name="oxf:identity">
						<p:input name="data">
							<error>Wrong login or password. Please try again.</error>
						</p:input>
						<p:output name="data" id="error"/>
					</p:processor>
				</p:otherwise>
			</p:choose>
		</p:when>
		<p:otherwise>
			<p:processor name="oxf:identity">
				<p:input name="data">
					<error/>
				</p:input>
				<p:output name="data" id="error"/>
			</p:processor>
		</p:otherwise>
	</p:choose>

	<p:processor name="oxf:identity">
		<p:input name="data" href="aggregate('data', #global_pref#xpointer(/globalpref/appname),#translation, #error, #success)"/>
		<p:output name="data" ref="data"/>
	</p:processor>
</p:config>